using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.UI.Design;
using System.Web.UI.WebControls;

namespace geoqualMenuBar
{
    public class GQMenuBarCollectionEditor : CollectionEditor
    {
        public GQMenuBarCollectionEditor(Type type)
            : base(type)
        {

        }

        protected override bool CanSelectMultipleInstances()
        {
            return true;
        }

        protected override Type CreateCollectionItemType()
        {
            return typeof(MenuBarItem);
        }
    }

    public class GQMenuBarControlDesigner : ControlDesigner 
    {
        public override bool AllowResize {
            get {
                return true;
            }
        }

        public override string GetDesignTimeHtml() {
            // Make the control more visible in the designer. If the border 
            // style is None or NotSet, change the border to an orange dotted line. 
            GQMenuBar gq = (GQMenuBar)Component;
            string markup = null;

            // Check if the border style should be changed.
            if (gq.Height == null || gq.Height == Unit.Pixel(0) && gq.Width == null || gq.Width == Unit.Pixel(0)) {
                // Set the design-time properties and catch any exceptions.
                try {
                    int intheight = 20;
                    if (gq.MenuBarItems != null && gq.MenuBarItems.Count > 0) {
                        int count = GetMenuItemsCount(gq.MenuBarItems, 0);
                        intheight = intheight * count;
                    }
                    int intwidth = 100;
                    if (gq.MenuBarItems != null && gq.MenuBarItems.Count > 0) {
                        int count = 1;
                        bool islevel = GetLevelsCount(gq.MenuBarItems, ref count);
                        count++;
                        if (count > 1) intwidth = intwidth * count;
                    }

                    string items = "";
                    if (gq.MenuBarItems != null && gq.MenuBarItems.Count > 0) {
                        items = " Items:";
                    }
                    // Call the base method to generate the markup.
                    markup = "<div style='border:1px solid black; width=" + intwidth.ToString() + "px; height=" + intheight.ToString() + "px; background-color: lightyellow;'><span>Horizontal MenuBar." + items + "</span>" + base.GetDesignTimeHtml() + "</div>";
                } catch (Exception) {
                    try {
                        markup = "<div style='border:1px solid black; width=100px; height=30px; background-color: lightyellow;'><span>Horizontal MenuBar.</span>" + base.GetDesignTimeHtml() + "</div>";
                    } catch (Exception ex) {
                        markup = GetErrorDesignTimeHtml(ex);
                    }
                }
            } else
                // Call the base method to generate the markup.
                markup = base.GetDesignTimeHtml();

            return markup;
        }

        private bool GetLevelsCount(MenuBars mb, ref int count) {
            bool isDeeper = false;
            if (mb != null && mb.Count > 0) {
                foreach (MenuBarItem mbi in mb) {
                    if (mbi.MenuBarItems != null && mbi.MenuBarItems.Count > 0) {
                        count++;
                        GetLevelsCount(mbi.MenuBarItems, ref count);
                        isDeeper = true;
                    }
                }
            }
            if (isDeeper) count--;
            return isDeeper;
        }

        private int GetMenuItemsCount(MenuBars mb, int count) {
            if (mb != null && mb.Count > 0) {
                foreach (MenuBarItem mbi in mb) {
                    count++;
                    if (mbi.MenuBarItems != null && mbi.MenuBarItems.Count > 0) {
                        count = GetMenuItemsCount(mbi.MenuBarItems, count);
                    }
                }
            }
            return count;
        }
    }
}


